home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9652 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: newsfeed.internetmci.com!panix!not-for-mail
  2. From: acinader@panix.com (Arthur Cinader Jr)
  3. Newsgroups: comp.lang.c++,gnu.g++.help
  4. Subject: Re: DONT UNDERSTAND - array of char error
  5. Date: 3 Mar 1996 09:50:22 -0500
  6. Organization: Panix
  7. Message-ID: <4hcbje$h52@panix.com>
  8. References: <4habvu$99j@panix.com> <4hahju$bil@news.BelWue.DE>
  9. NNTP-Posting-Host: panix.com
  10.  
  11. In <4hahju$bil@news.BelWue.DE> kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl) writes:
  12.  
  13. Dietmar,
  14.  
  15. Wow, thanks.  I love the internet and thank you for your kind
  16. assistance!
  17.  
  18. >Well, because you forgot about an important restriction on arrays: All
  19. >but the first dimension has to be fixed at compile-time.
  20.  
  21. >: In the definition (.C) file, I use the comand:
  22.  
  23. >:     labels = new char[maxargs][fieldlength]; // make label array
  24. >:     params = new char[maxargs][fieldlength]; // make params array
  25.  
  26. >... i.e. 'fieldlength' has to be a 'const'. If 'fieldlength' is
  27. >non-const you have to allocate the second dimension by hand (unless you
  28. >use an array class like suggested below):
  29.  
  30. I thought I covered this by making 
  31.  
  32.     const int fieldlength;
  33.  
  34. and using a member intializer like this:
  35.  
  36.     Param::Param(char *shape)
  37.         :maxargs(10), fieldlength(20)
  38.  
  39. In any event, what you showed me below is what I wanted to
  40. do, but couldn't come up with on my own...
  41.  
  42. >  lables = new char *[maxargs]; // it is valid to allocate an array
  43. >  params = new char *[maxargs]; // of 'char*'
  44. >  for (int i = 0; i < maxargs; i++)
  45. >  {
  46. >    labels[i] = new char[fieldlength]; // ... and to allocate an
  47. >    params[i] = new char[fieldlength]; // of 'char'.
  48. >  }
  49.  
  50. >... but an array thus create has to be destructed like this:
  51.  
  52. >  for (int i = maxargs; i--; )
  53. >  {
  54. >    delete[] params[i];
  55. >    delete[] lables[i];
  56. >  }
  57. >  delete params;
  58. >  delete lables;
  59.  
  60. >Which is, IMHO, far to dangerous to be done: Given that you run out of
  61. >memory, you are likely to end up with a serious memory-leak. I know how
  62. >to program in C++ and I won't do it like this... This is exactly the
  63. >problem I have with all C++ tutorials I have seen until know: They
  64. >introduce the low-level methods, requiring to be an expert to handle
  65. >them correctly, but they don't introduce the clean methods provided by
  66. >the (upcoming) ISO Standard C++ Library (to be fair: this library was
  67. >introduced into the standard quite recently, i.e. sometime in 1994). A
  68. >clean method is to use the 'vector' class found in libg++ starting with
  69. >libg++-2.6.? (you didn't mention what version you are using but I
  70. >assume a recent one, e.g. 2.7.2):
  71.  
  72.  
  73. Your point noted, but I am going to risk it in this case.  I
  74. am overwhelmed enough without having to stop now to figure out
  75. how to use the libraries and understand their interface.  I do
  76. recognize this as a crucial part of my becoming productive and
  77. I will endevour to find documentation on the ISO standard
  78. library and learn about how to use it.  FWIW the next chapter
  79. in the text is on I/O streams which I believe spends a
  80. considerable amount of time explaining "pre-packaged" class
  81. hiararchies and how to navigate them.
  82.  
  83.  
  84. Thanks again Dietmar.  
  85.  
  86. Best Regards,
  87.  
  88. Arthur
  89.